package com.htudata.core.util.operation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CollectionsOperationUtils {

  
  public static boolean isValidCollection(Collection collection) {
    if (collection != null && 
      !collection.isEmpty())
      return true; 
    return false;
  }

  public static <T> void sortByLongField(List<T> list, final String sortField, final String sortType) throws Exception{
    Collections.sort(list, new Comparator<T>() {
          public int compare(Object obj1, Object obj2) {
            T t1 = (T)obj1;
            T t2 = (T)obj2;
              Long value1 = 0L;
              try {
                  value1 = (Long) ClassOperationUtils.executeGetter(t1, sortField, new Class[0]);
              } catch (Exception e) {
                  throw new RuntimeException(e);
              }
              Long value2 = 0L;
              try {
                  value2 = (Long) ClassOperationUtils.executeGetter(t2, sortField, new Class[0]);
              } catch (Exception e) {
                  throw new RuntimeException(e);
              }
              int result = 0;
            if ("asc".equals(sortType)) {
              result = value1.compareTo(value2);
              return result;
            } 
            if ("desc".equals(sortType)) {
              result = value2.compareTo(value1);
              return result;
            } 
            return result;
          }
        });
  }
}
